package thread.unit1.exetend;

/**
 * 两个线程交替进行
 * 一会跑A  一会跑
 */
public class ThreadW1{
    static Object object = new Object();

    public static void main(String[] args) {
        A a = new A();
        B b = new B();
        C c= new C();
        Thread t1 = new Thread(a);
        Thread t2 = new Thread(b);
        Thread t3 = new Thread(c);
        t1.setName("线程A");
        t2.setName("线程B");
        t3.setName("线程C");
        t1.start();
        t2.start();
        t3.start();

        new Thread(){
        }.start();
    }
    static class A implements Runnable{
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                synchronized (object){
                    System.out.println(Thread.currentThread().getName()+"--执行");
                    object.notify();
                    try {
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    static class B implements Runnable{
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                synchronized (object){
                    System.out.println(Thread.currentThread().getName()+"--执行");
                    object.notify();
                    try {
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    static class C implements Runnable{
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                synchronized (object){
                    System.out.println(Thread.currentThread().getName()+"--执行");
                    object.notify();
                    try {
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}


